package org.edu.modules.kaoqin.service.impl;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Week;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.edu.modules.kaoqin.dto.VeHrPerformanceDTO;
import org.edu.modules.kaoqin.entity.VeHrPerformance;
import org.edu.modules.kaoqin.entity.VeHrPerformanceRule;
import org.edu.modules.kaoqin.mapper.VeHrPerformanceMapper;
import org.edu.modules.kaoqin.service.IVeHrPerformanceRuleService;
import org.edu.modules.kaoqin.service.IVeHrPerformanceService;
import org.edu.modules.kaoqin.vo.PerformanceUserVO;
import org.edu.modules.kaoqin.vo.WeekStatisticVO;
import org.edu.modules.teacher.entity.BaseTeacher;
import org.edu.modules.teacher.mapper.BaseTeacherMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class VeHrPerformanceServiceImpl extends ServiceImpl<VeHrPerformanceMapper, VeHrPerformance> implements IVeHrPerformanceService {

    @Resource
    private VeHrPerformanceMapper performanceMapper;
    @Autowired
    private IVeHrPerformanceRuleService performanceRuleService;
    @Resource
    private BaseTeacherMapper teacherMapper;

    @Override
    public Page<VeHrPerformanceDTO> listByCondition(Page<VeHrPerformanceDTO> page, VeHrPerformanceDTO performanceDTO) {
        page = performanceMapper.listByCondition(page, performanceDTO);
        List<VeHrPerformanceRule> list = performanceRuleService.list();
        Map<String, BigDecimal> collect = list.stream().collect(Collectors.toMap(VeHrPerformanceRule::getName, VeHrPerformanceRule::getProportion));

        for (VeHrPerformanceDTO record : page.getRecords()) {
            BigDecimal zbTotal = collect.getOrDefault("值班", BigDecimal.ONE).multiply(new BigDecimal(record.getZb()));
            record.setZbTotal(zbTotal);
            BigDecimal swTotal = collect.getOrDefault("上午", BigDecimal.ONE).multiply(new BigDecimal(record.getSw()));
            record.setSwTotal(swTotal);
            BigDecimal xwTotal = collect.getOrDefault("下午", BigDecimal.ONE).multiply(new BigDecimal(record.getXw()));
            record.setXwTotal(xwTotal);
            BigDecimal wsTotal = collect.getOrDefault("晚上", BigDecimal.ONE).multiply(new BigDecimal(record.getWs()));
            record.setWsTotal(wsTotal);
            record.setTotal(zbTotal.add(swTotal).add(xwTotal).add(wsTotal));
        }
        return page;
    }

    @Override
    public Object listByWeekStatistic(Page<VeHrPerformanceDTO> page, VeHrPerformanceDTO performanceDTO) {

        if (performanceDTO.getKqrq() == null) {
            performanceDTO.setKqrq(new Date());
        }

        // 周日期范围
        Date start = DateUtil.beginOfWeek(DateUtil.calendar(performanceDTO.getKqrq())).getTime();
        Date end = DateUtil.endOfWeek(DateUtil.calendar(performanceDTO.getKqrq())).getTime();

        // 先查询用户
        Page<BaseTeacher> teacherPage = new Page<BaseTeacher>(page.getCurrent(), page.getSize());
        Page<BaseTeacher> baseTeacherList = teacherMapper.selectPage(teacherPage, new LambdaQueryWrapper<BaseTeacher>()
                .like(StrUtil.isNotBlank(performanceDTO.getXm()), BaseTeacher::getXm, performanceDTO.getXm())
                .eq(null != performanceDTO.getDepartId(), BaseTeacher::getDepid, performanceDTO.getDepartId()));

        // 查询规则
        List<VeHrPerformanceRule> list = performanceRuleService.list();
        Map<String, BigDecimal> collect = list.stream().collect(Collectors.toMap(VeHrPerformanceRule::getName, VeHrPerformanceRule::getProportion));

        // 返回数据的数组
        ArrayList<PerformanceUserVO> performanceUserVOS = new ArrayList<>();

        // 查询用户一周内所有考勤记录
        for (BaseTeacher record : baseTeacherList.getRecords()) {
            List<VeHrPerformance> weekList = this.list(new LambdaQueryWrapper<VeHrPerformance>()
                    .eq(VeHrPerformance::getUserid, record.getUserid())
                    .between(VeHrPerformance::getKqrq, start, end));

            PerformanceUserVO performanceUserVO = new PerformanceUserVO();
            performanceUserVO.setDepid(record.getDepid());
            performanceUserVO.setUserid(record.getUserid());

            ArrayList<WeekStatisticVO> tempWeekStatisticVOS = new ArrayList<>();

            // 分别计算每天的总计(不一定是完整的周，只有考勤天的记录)
            for (VeHrPerformance performance : weekList) {
                WeekStatisticVO weekStatisticVO = new WeekStatisticVO();
                BigDecimal zbTotal = collect.getOrDefault("值班", BigDecimal.ONE).multiply(new BigDecimal(performance.getZb()));
                BigDecimal swTotal = collect.getOrDefault("上午", BigDecimal.ONE).multiply(new BigDecimal(performance.getSw()));
                BigDecimal xwTotal = collect.getOrDefault("下午", BigDecimal.ONE).multiply(new BigDecimal(performance.getXw()));
                BigDecimal wsTotal = collect.getOrDefault("晚上", BigDecimal.ONE).multiply(new BigDecimal(performance.getWs()));
                BigDecimal add = zbTotal.add(swTotal).add(xwTotal).add(wsTotal);
                weekStatisticVO.setDayTotal(add.doubleValue());
                weekStatisticVO.setKqrq(performance.getKqrq());
                tempWeekStatisticVOS.add(weekStatisticVO);
            }
            double sum = tempWeekStatisticVOS.stream().mapToDouble(WeekStatisticVO::getDayTotal).sum();

            // 给前端完整的周7条记录
            Map<Date, WeekStatisticVO> weekStatisticVOMap = tempWeekStatisticVOS.stream().collect(Collectors.toMap(WeekStatisticVO::getKqrq, x -> x));
            ArrayList<WeekStatisticVO> weekStatisticVOS = new ArrayList<>();
            for (int i = 0; i < 7; i++) {
                WeekStatisticVO weekStatisticVO = new WeekStatisticVO();
                DateTime dateTime = DateUtil.offsetDay(start, i);
                Week week = DateUtil.dayOfWeekEnum(dateTime);

                WeekStatisticVO orDefault = weekStatisticVOMap.getOrDefault(dateTime, new WeekStatisticVO());
                BeanUtils.copyProperties(orDefault, weekStatisticVO);
                weekStatisticVO.setKqrq(dateTime);
                weekStatisticVO.setWeekName(week.toChinese("周"));
                weekStatisticVOS.add(weekStatisticVO);
            }


            // 周总计
            performanceUserVO.setWeekTotal(sum);
            performanceUserVO.setWeekStatisticVOS(weekStatisticVOS);
            performanceUserVOS.add(performanceUserVO);
        }

        Page<PerformanceUserVO> performanceUserVOPage = new Page<>();
        performanceUserVOPage.setCurrent(teacherPage.getCurrent());
        performanceUserVOPage.setCountId(teacherPage.getCountId());
        performanceUserVOPage.setSize(teacherPage.getSize());
        performanceUserVOPage.setTotal(teacherPage.getTotal());
        performanceUserVOPage.setRecords(performanceUserVOS);
        return performanceUserVOPage;
    }
}